package com.ingenio.siot.servlet;


import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.ingenio.siot.Helper;
import com.ingenio.siot.adapter.ReportesAdapter;
import com.ingenio.siot.entity.Top10LineasEstado;

/**
 * Servlet implementation class DevolverMovilServlet
 */
@WebServlet("/Top10LineasPorEstado")
public class Top10LineasPorEstadoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Top10LineasPorEstadoServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		JSONObject json = new JSONObject();
		if (request.getParameter("idCriterio") != null && Helper.isInteger(request.getParameter("idCriterio"))
				&& request.getParameter("idEstado") != null && Helper.isInteger(request.getParameter("idEstado"))
				&& request.getParameter("dias") != null && Helper.isInteger(request.getParameter("dias"))){
			try {
				Integer criterio = Integer.parseInt(request.getParameter("idCriterio"));
				Integer estado = Integer.parseInt(request.getParameter("idEstado"));
				Integer dias = Integer.parseInt(request.getParameter("dias"));
				
				ReportesAdapter ra = new ReportesAdapter();
				List<Top10LineasEstado> lineas = ra.devolverTop10LineasPorEstado(dias, criterio, estado);
				
				JSONArray ja = new JSONArray();
				for(Top10LineasEstado top10linea:lineas){
					JSONObject jo = new JSONObject();
					jo.put("idAltura", top10linea.getLinea().getAlturaInicioDibujo().getIdAltura());
					jo.put("calleAlt", top10linea.getLinea().getAlturaInicioDibujo().getCalle().getDescCalle() + " " +top10linea.getLinea().getAlturaInicioDibujo().getNroAltura());
					jo.put("cantApariciones", top10linea.getCantApariciones());
					//jo.put("color", lmt.getTipoCalle().getColorGrafico()); me va a faltar el color para pasarle al grafico!
					ja.put(jo);
				}
				json.put("lista", ja);
				response.setContentType("application/json");
				response.getWriter().write(json.toString());
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}else{
			try {
				json.put("error", true);
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
	}
}
